package com.cao.springbootinit.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cao.springbootinit.annotation.ScoringStrategyConfig;
import com.cao.springbootinit.common.ErrorCode;
import com.cao.springbootinit.exception.BusinessException;
import com.cao.springbootinit.model.dto.question.QuestionContenDto;
import com.cao.springbootinit.model.entity.App;
import com.cao.springbootinit.model.entity.Question;
import com.cao.springbootinit.model.entity.ScoringResult;
import com.cao.springbootinit.model.entity.UserAnswer;
import com.cao.springbootinit.model.vo.QuestionVO;
import com.cao.springbootinit.service.QuestionService;
import com.cao.springbootinit.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * * 自定义评分策略
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {
    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer calculate(List<String> choices, App app) throws Exception {
        //1.根据 ID 查询到题目和结果信息(按分数降序排序)
        Long appId = app.getId();
        if (appId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "应用id不能为空");
        }
        //1.根据id查询题目和题目结果信息
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId)
        );
        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appId
                        ).orderByDesc(ScoringResult::getResultScoreRange));
        //2.统计用户得分
        int totalScore = 0;
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContenDto> questionContent = questionVO.getQuestionContent();
        for (int i = 0; i < questionContent.size(); i++) {

            for (QuestionContenDto questionContenDto : questionContent) {
                //便利答案列表
                for (String choice : choices) {
                    //遍历题目中的选项
                    for (QuestionContenDto.Optional optional : questionContenDto.getOptions()) {
                        //如果答案和题目key属性匹配
                        if (optional.getKey().equals(choice)) {
                            int score = Optional.of(optional.getScore()).orElse(0);
                            totalScore += score;

                        }
                    }
                }
            }
        }

        //3.遍历得分得分结果,找到第一个用户分数大于范围结果，作为最终结果
        ScoringResult MaxscoringResult = scoringResultList.get(0);
        for (ScoringResult scoringResult : scoringResultList) {
            if (totalScore >= scoringResult.getResultScoreRange()) {

                MaxscoringResult = scoringResult;
                break;
            }
        }
        //4.返回结果
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(MaxscoringResult.getId());
        userAnswer.setResultName(MaxscoringResult.getResultName());
        userAnswer.setResultDesc(MaxscoringResult.getResultDesc());
        userAnswer.setResultPicture(MaxscoringResult.getResultPicture());
        return userAnswer;
    }
}
